// 路由鉴权

// 引入路由器
import router from './router/index'
//引入进度条
import Nprogress from 'nprogress'
//引入进度条的样式
import 'nprogress/nprogress.css'
//进度条的加载小圆球不要
// Nprogress.configure({ showSpinner: false });
//引入用户相关的仓库
import useUserStore from '@/store/modules/user'
//引入大仓库
import pinia from '@/store'
let userStore = useUserStore(pinia)

//存储用户未登录可以访问路由得路径
let whiteList = [
  '/home',
  '/hospital/register',
  '/hospital/detail',
  '/hospital/notice',
  '/hospital/close',
  '/hospital/search',
]

// 添加相应的全局守卫
// 前置守卫
router.beforeEach((to, from, next) => {
  // 访问路由组件之前
  Nprogress.start()
  document.title = `尚医通-${to.meta.title}`
  // 判断用户是否登录
  if (userStore.userInfo.token) {
    next()
  } else {
    // 用户未登录
    if (whiteList.includes(to.path)) {
      next()
    } else {
      //登录组件显示
      userStore.visiable = true
      next({ path: '/home', query: { redirect: to.fullPath } })
    }
  }
})

// 后置守卫
router.afterEach((to, from) => {
  // 访问路由组件之后
  Nprogress.done()
})
